package novel.spider.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import novel.spider.entity.Chapter;
import novel.spider.enums.NovelSiteEnum;
import novel.spider.face.IChapterSpider;
import novel.spider.util.NovelSpiderUtil;

public abstract class  AbstractChapterSpider implements IChapterSpider {

	protected String crawl(String url) throws Exception {
		try (CloseableHttpClient client = HttpClientBuilder.create().build();
				CloseableHttpResponse httpResponse = client.execute(new HttpGet(url))) {
			String result = EntityUtils.toString(httpResponse.getEntity(),NovelSpiderUtil.getContext(NovelSiteEnum.getEnumByUrl(url)).get("charset"));
			return result; 
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Chapter> getsChapter(String url) {
		try {
			String result = crawl(url);
			Document doc = Jsoup.parse(result);
			Elements aElements = doc.select(NovelSpiderUtil.getContext(NovelSiteEnum.getEnumByUrl(url)).get("chapter-list-selector"));
				
			List<Chapter>chapters = new ArrayList<>();
			for(Element a :aElements){
				Chapter chapter = new Chapter();
				chapter.setTitle(a.html());
				chapter.setUrl(url+a.attr("href"));
				chapters.add(chapter);
			}

			return chapters;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
